﻿namespace _16_字典;

internal class Program
{
    private static void Main(string[] args)
    {
        #region 1_字典初始化器

        //var domains = new Dictionary<string, string>
        //{
        //    { "sk", "Slovakia"}, 
        //    { "ru", "Russia"},
        //    { "de", "Germany"},
        //    { "no", "Norway"}
        //}; // 相当于json字符串
        //Console.WriteLine(domains["sk"]);

        //var days = new Dictionary<string, string>
        //{
        //    ["mo"] = "Monday",
        //    ["tu"] = "Tuesday",
        //    ["we"] = "Wednesday",
        //    ["th"] = "Thursday",
        //    ["fr"] = "Friday",
        //    ["sa"] = "Saturday",
        //    ["su"] = "Sunday"
        //}; // 类似js中的对象, 可以使用中括号
        //Console.WriteLine(days["mo"]);

        #endregion

        #region 2_计数元素

        //Console.WriteLine("2_计数元素");
        //var keyValues = new Dictionary<string, string>
        //{
        //    { "sk", "Slovakia"},
        //    { "ru", "Russia"},
        //    { "de", "Germany"},
        //    { "no", "Norway"}
        //};
        //Console.WriteLine($"There are {keyValues.Count} elements");

        #endregion

        #region 3_删除或者添加元素

        //var users = new Dictionary<string, int>()
        //{
        //    { "John Doe", 41 },
        //    { "Jane Doe", 38 },
        //    { "Lucy Brown", 29 },
        //};

        //users["Paul Brown"] = 33;
        //users.Add("Thomas Pattison", 34);

        //Console.WriteLine(string.Join(", ", users));

        //users.Remove("Jane Doe");

        //Console.WriteLine(string.Join(", ", users));

        //users.Clear();

        //if (users.Count == 0)
        //{
        //    Console.WriteLine("The users dictionary is empty");
        //}

        #endregion

        #region 4_ContainsKey和ContainsValue方法

        //// ContainsKey()方法确定字典中是否包含指定的键, 而ContainsValue方法则是确定字典中是否包含指定的值
        //var domains = new Dictionary<string, string>
        //{
        //    {"sk", "Slovakia"},
        //    {"ru", "Russia"},
        //    {"de", "Germany"},
        //    {"no", "Norway"}
        //};

        //var key = "sk";

        //if (domains.ContainsKey(key))
        //{
        //    Console.WriteLine($"The {key} key is in the dictionary");
        //}
        //else
        //{
        //    Console.WriteLine($"The {key} key is in not the dictionary");
        //}

        //var value = "Slovakia";

        //if (domains.ContainsValue(value))
        //{
        //    Console.WriteLine($"The {value} value is in the dictionary");
        //}
        //else
        //{
        //    Console.WriteLine($"The {value} value is in not the dictionary");
        //}

        #endregion

        #region 5_遍历字典

        //var domains = new Dictionary<string, string>
        //{
        //    {"sk", "Slovakia"},
        //    {"ru", "Russia"},
        //    {"de", "Germany"},
        //    {"no", "Norway"}
        //};

        //foreach (var kvp in domains)
        //{
        //    Console.WriteLine($"{kvp.Key} -> {kvp.Value}");
        //}

        //foreach(var (key, value) in domains)
        //{
        //    Console.WriteLine($"{key} -> {value}");
        //}

        //foreach (var key in domains.Keys) // key
        //{
        //    Console.WriteLine($"{key}");
        //}
        //foreach (var value in domains.Values) // value
        //{
        //    Console.WriteLine($"{value}");
        //}

        #endregion

        #region 6_排序字典->使用LinQ

        //var users = new Dictionary<string, int>
        //{
        //    { "John Doe", 41 },
        //    { "Jane Doe", 38 },
        //    { "Lucy Brown", 29 },
        //    { "Paul Brown", 33 },
        //    { "Thomas Pattison", 34 }
        //};
        //// 默认是升序, 如果希望使用降序, 则需要使用OrderByDescending方法
        //var sortedUserByValue = users.OrderBy(user => user.Value);
        //foreach (var user in sortedUserByValue)
        //{
        //    Console.WriteLine($"{user.Key} -> {user.Value}");
        //}
        //var sortedDescUserByValue = users.OrderByDescending(user => user.Value);
        //// 降序
        //foreach (var user in sortedDescUserByValue)
        //{
        //    Console.WriteLine($"{user.Key} -> {user.Value}");
        //}

        #endregion

        #region 7_SortedDictionary

        //var sortedUsers = new SortedDictionary<string, int>()
        //{
        //    { "John", 41 },
        //    { "Jane", 38 },
        //    { "Lucy", 29 },
        //    { "Paul", 24 }
        //};

        //foreach (var user in sortedUsers)
        //{
        //    Console.WriteLine($"{user.Key} is {user.Value} years old");
        //}

        #endregion
    }
}